package controller;

import remoteEntity.Download;
import remoteEntity.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import manager.LogManager;
import manager.UserManager;

/**
 * ProfileController
 *
 * JSF Managed backing bean containing model viewing logic for a user for the
 * profile page. Also allow administrators to promote/demote other users to/from
 * user/administrator.
 */
@ManagedBean
@ViewScoped
public class ProfileController {

    @EJB
    private UserManager userManager;
    @EJB
    private LogManager logManager;
    @ManagedProperty(value = "#{userController}")
    private UserController userController;
    private Integer id;

    public UserController getUserController() {
        return userController;
    }

    public void setUserController(UserController userController) {
        this.userController = userController;
    }

    public ProfileController() {
    }

    @PostConstruct
    public void init() {
        String stringId = FacesContext.getCurrentInstance().
                getExternalContext().getRequestParameterMap().get("id");
        if (stringId == null) {
            this.id = null;
        } else {
            this.id = Integer.valueOf(stringId);
        }
    }

    public User getUser() {
        if (this.id == null) {
            return userController.getUser();
        } else {
            return userManager.getUserById(this.id);
        }
    }

    public List<Download> getDownloads() {
        if (this.id == null) {
            return logManager.getDownladsForUser(userController.getUser().getId());
        } else {
            return logManager.getDownladsForUser(this.id);
        }
    }

    public boolean getIsAdmin() {
        return userManager.getIsAdmin(id);
    }

    public boolean getShouldShowPromote() {
        if (userController.getIsAdmin() && this.id != null) {
            return true;
        } else {
            return false;
        }
    }

    public void promote() {
        if (userController.getIsAdmin()) {
            userManager.promote(id);
            logManager.logInfo("profile", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") promoted user id " + id + ".");
        } else {
            logManager.logWarning("profile", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") tried to promote user id " + id + " but does not have authority.");
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("profile?id=" + id);
        } catch (Exception e) {
        }
    }

    public void demote() {
        if (userController.getIsAdmin()) {
            userManager.demote(id);
            logManager.logInfo("profile", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") demoted user id " + id + ".");
        } else {
            logManager.logWarning("profile", "User " + userController.getUser().getUsername()
                    + " (id " + userController.getUser().getId() + ") tried to demote user id " + id + " but does not have authority.");
        }
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("profile?id=" + id);
        } catch (Exception e) {
        }
    }
}
